// This template will be used for inserting the MCQs
// The window will display a question, 4 options. Content writers
// shud be able to define the correct answer option and submit.
// author vinil.bhandari@kaplan.com

var win = Titanium.UI.currentWindow;
var scroll = Titanium.UI.createScrollView({
    contentHeight:1000,
});
win.barColor = 'null';


win.backgroundColor = '#6ED0F1';
var whiteBackground = Ti.UI.createView({ width :'100%', height : 63, top : -4, zIndex : 1, backgroundColor: 'white',borderWidth:2,borderColor:'#aaa' }); 
var Logo = Ti.UI.createImageView({ width :'100%', height : 55, top : 0,  zIndex : 1, image : '../images/dashboard/kaptest.png' }); 

win.add(Logo);
win.add(whiteBackground);


	// Add data to the database table
	
	if(win.info==null){
		var dbase = Titanium.Database.open('gyaan');
		var myResultSetQ = dbase.execute('SELECT max(id) as id FROM questions');
		var maxId =0;
		while (myResultSetQ.isValidRow()) {
			maxId = myResultSetQ.fieldByName('id');
			myResultSetQ.next();
		}
		myResultSetQ.close();
	maxId = maxId + 1;
	dbase.close();
	}
	else
	var maxId=win.info;
	
	


	var view2 = Ti.UI.createView({
	backgroundColor:'#6ED0F1'
});
win.add(view2);

var topic = Titanium.UI.createLabel({
        text:"Define Learning Objective:",
        width:'85%',
        height:20,
        top : 65,
        font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
        textAlign:'left',
        color:"#000000"
    });
scroll.add(topic);
var top = Titanium.UI.createTextArea({
    value : '',
    height : 40,
    right:1,
    width : '90%',
    top : 85,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 5
});

scroll.add(top);


var stem = Titanium.UI.createLabel({
        text:"Define Question Stem:",
        width:'85%',
        height:20,
        top : 65+75,
        font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
        textAlign:'left',
        color:"#000000"
    });
scroll.add(stem);
var ta1 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    right:1,
    width : '90%',
    top : 85+75,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 5
});

var ans = Titanium.UI.createLabel({
        text:"Add answer options:",
        width:'85%',
        height:20,
        font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
        textAlign:'left',
        top : 180+75,
        color:"#000000"
    });

var opt1 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 200+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});

var opt2 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 290+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});

var opt3 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 380+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});

var opt4 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 470+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    
    
    
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});


var opt5 = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 560+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});

var aeLabel = Titanium.UI.createLabel({
        text:"Add explanations:",
        width:'85%',
        height:20,
        font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
        textAlign:'left',
        top : 720,
        color:"#000000"
    });

var ae = Titanium.UI.createTextArea({
    value : '',
    height : 80,
    width : '90%',
    top : 680+75,
    right:1,
    font : {fontSize:20,fontFamily:'Arial', fontWeight:'bold'},
    color : '#000000',
    textAlign : 'left',
    borderWidth : 2,
    borderColor : '#bbb',
    borderRadius : 20
});

scroll.add(ae);
scroll.add(aeLabel);


	var b9 = Titanium.UI.createButton({
		title:'Submit',
		style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
		color:'#463E77'
	});
var record = Ti.UI.createButton({
	backgroundImage:'../images/dashboard/movie_off.png',
	height:33,
	width:33
});


var pic = Ti.UI.createButton({
	backgroundImage:'../images/camera.png',
	height:33,
	width:33,
	zIndex:3
});


pic.addEventListener('click', function (e){
	Ti.Media.openPhotoGallery({
	    success:function(event)
	    {
	        Ti.API.info('success'+e.media);
	    },
	    cancel:function()
	    {
	        Ti.API.info('...canceled');
	    },
	    error:function(error)
	    {
	        Ti.API.info('error');
	    },
	    allowImageEditing:true
	});
});



if (Titanium.Platform.name == 'iPhone OS'){
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	win.setToolbar([flexSpace,flexSpace,b9],{translucent:true});
	win.rightNavButton = record;
	win.leftNavButton = pic;
}else{
	stem.height=stem.height+30;
	ta1.top=ta1.top+20;
	ans.top=ans.top+20;
	ans.height=ans.height+20;
	opt1.top=opt1.top+40;
	opt2.top=opt2.top+40;
	opt3.top=opt3.top+40;
	opt4.top=opt4.top+40;
	opt5.top=opt5.top+40;
	
	b9.bottom=10;
	b9.height=60;
	b9.width=240;
	b9.left=80;
	scroll.add(b9);
	record.bottom=50;
	record.right=10;
	record.width=record.width+50;
	record.height=record.height+50;
	pic.top=15;
	pic.left=10;
	win.add(pic);
	win.add(record);
}

b9.addEventListener('click', function()
	{	
		var dbase = Titanium.Database.open('gyaan');
	var answer = checked1?'A':checked2?'B':checked3?'C':checked4?'D':checked5?'E':'';
	if(answer=='' || answer==null){
		alert("Please pick a correct option using radio buttons.");
		return;
	}
	if(ta1.value=='' || ta1.value==null){
		alert("Please define question stem.");
		return;
	}
	dbase.execute('INSERT INTO questions (id, title, actual_answer, type, lesson_id, subject, topic, exp ) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', ''+parseInt(maxId),  ''+ta1.value, ''+answer,'Number Properties',1, 'Dummy subject','Dummy topic', ''+ae.value);
	dbase.execute('INSERT INTO options (id, q_id, a, b, c, d, e) VALUES(?, ?, ?, ?, ?, ?, ?)', ''+parseInt(maxId),  ''+parseInt(maxId), ''+opt1.value,''+opt2.value,''+opt3.value, ''+opt4.value,''+opt5.value);
	dbase.close();
	
	ta1.value = '';
	opt1.value = '';
	opt2.value = '';
	opt3.value = '';
	opt4.value = '';
	opt5.value = '';
	var newWindow = Titanium.UI.createWindow({  
	    title:'Add Questions',
	    url: 'qBank.js',
	    info: maxId+1
	});
	newWindow.animate({view:view2, transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
	Titanium.UI.currentTab.open(newWindow,{animated:true});
	
	
	alert('Question submitted. You can practice it via Practice tab');	
	});
record.addEventListener('click', function (e){
	if (Titanium.Platform.name != 'iPhone OS'){
	
	}
	
  		Titanium.Media.showCamera({
			success:function(event)
			{
				var video = event.media;
				movieFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mymovie'+maxId+'.mov');
				movieFile.write(video);
			},
			cancel:function()
			{
			},
			error:function(error)
			{
				// create alert
				var a = Titanium.UI.createAlertDialog({title:'Video'});

				// set message
				if (error.code == Titanium.Media.NO_VIDEO)
				{
					a.setMessage('Device does not have video recording capabilities');
				}
				else
				{
					a.setMessage('Unexpected error: ' + error.code);
				}

				// show alert
				a.show();
			},
			mediaTypes: Titanium.Media.MEDIA_TYPE_VIDEO,
			videoMaximumDuration:60000,
			videoQuality:Titanium.Media.QUALITY_HIGH
		});
 
});

scroll.add(ta1);
scroll.add(ans);
scroll.add(opt1);
scroll.add(opt2);
scroll.add(opt3);
scroll.add(opt4);
scroll.add(opt5);



var checked1=false;
var checked2=false;
var checked3=false;
var checked4=false;
var checked5=false;

var radio1 = Titanium.UI.createView({ width: 50, height: 50,  left: 15,top:215+75, zIndex:2, backgroundImage: '../images/radio_uncheck.png' });
var radio2 = Titanium.UI.createView({ width: 50, height: 50,  left: 15,top:305+75, zIndex:2,backgroundImage: '../images/radio_uncheck.png' });
var radio3 = Titanium.UI.createView({ width: 50, height: 50,  left: 15,top:405+65, zIndex:2,backgroundImage: '../images/radio_uncheck.png' });
var radio4 = Titanium.UI.createView({ width: 50, height: 50,  left: 15,top:475+85, zIndex:2,backgroundImage: '../images/radio_uncheck.png' });
var radio5 = Titanium.UI.createView({ width: 50, height: 50,  left: 15,top:575+75, zIndex:2,backgroundImage: '../images/radio_uncheck.png' });
radio1.addEventListener('click', function (e){
  	radio1.backgroundImage = '../images/radio_check.png';
    radio2.backgroundImage = '../images/radio_uncheck.png';
    radio3.backgroundImage = '../images/radio_uncheck.png';
    radio4.backgroundImage = '../images/radio_uncheck.png';
    radio5.backgroundImage = '../images/radio_uncheck.png';
     checked1=true;
	 checked2=false;
	 checked3=false;
	 checked4=false;
	 checked5=false;
});

radio2.addEventListener('click', function (e){
  	radio1.backgroundImage = '../images/radio_uncheck.png';
    radio2.backgroundImage = '../images/radio_check.png';
    radio3.backgroundImage = '../images/radio_uncheck.png';
    radio4.backgroundImage = '../images/radio_uncheck.png';
    radio5.backgroundImage = '../images/radio_uncheck.png';
     checked1=false;
	 checked2=true;
	 checked3=false;
	 checked4=false;
	 checked5=false;
 
});

radio3.addEventListener('click', function (e){
  	radio1.backgroundImage = '../images/radio_uncheck.png';
    radio2.backgroundImage = '../images/radio_uncheck.png';
    radio3.backgroundImage = '../images/radio_check.png';
    radio4.backgroundImage = '../images/radio_uncheck.png';
    radio5.backgroundImage = '../images/radio_uncheck.png';
     checked1=false;
	 checked2=false;
	 checked3=true;
	 checked4=false;
	 checked5=false;
 
});

radio4.addEventListener('click', function (e){
  	radio1.backgroundImage = '../images/radio_uncheck.png';
    radio2.backgroundImage = '../images/radio_uncheck.png';
    radio3.backgroundImage = '../images/radio_uncheck.png';
    radio4.backgroundImage = '../images/radio_check.png';
    radio5.backgroundImage = '../images/radio_uncheck.png';
     checked1=false;
	 checked2=false;
	 checked3=false;
	 checked4=true;
	 checked5=false;
  
});

radio5.addEventListener('click', function (e){
  	radio1.backgroundImage = '../images/radio_uncheck.png';
    radio2.backgroundImage = '../images/radio_uncheck.png';
    radio3.backgroundImage = '../images/radio_uncheck.png';
    radio4.backgroundImage = '../images/radio_uncheck.png';
    radio5.backgroundImage = '../images/radio_check.png';
     checked1=false;
	 checked2=false;
	 checked3=false;
	 checked4=false;
	 checked5=true;
  
});


scroll.add(radio1);
scroll.add(radio2);
scroll.add(radio3);
scroll.add(radio4);
scroll.add(radio5);
win.add(scroll);

win.add(scroll);

 var subject = Titanium.UI.createWebView({
			url :  '../html/dropdown.html',
			width : '100%' ,
			height : 400,
			top : 10,
			height:30,
			left:10,
			zIndex : 3	
		});
 win.add(subject);

 
var number = Titanium.UI.createLabel({
        text:"# "+maxId,
        height:30,
        right:10,
        font : {fontSize:30,fontFamily:'Arial', fontWeight:'bold'},
         top : 15,
         zIndex : 10,
         textAlign:'right',
        color:"#000000"
    });
win.add(number);